# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=newlib-esp
# Ideally those Espressif guys would upstream there changes or at least
# follow the upstreams versioning. But neither is the case. We fall back
# to use the date of the most recent commit as version :/
pkgver=0_git20240109
_tag=4.3.0_20240109
_xtensa_overlay_ver=2021r2-patch4
pkgrel=0
pkgdesc="Espressif's fork of newlib, a standard C lib for ESP MCUs"
url="https://github.com/espressif/newlib-esp32"
makedepends="texinfo"
arch="aarch64 x86_64" # limited by binutils-xtensa*, gcc-xtensa-*
license="GPL-2.0-only AND LGPL-2.1-only AND 0BSD AND BSD-3-Clause AND GPL-3.0-only AND LGPL-3.0-only"
source="
	newlib-esp-$_tag.tar.gz::https://github.com/espressif/newlib-esp32/archive/refs/tags/esp-$_tag.tar.gz

	0001-getentropy-fix-compilation.patch
	"
options="!check"

_targets="
	xtensa-none-elf
	xtensa-esp8266-elf
	xtensa-esp32-elf
	xtensa-esp32s2-elf
	xtensa-esp32s3-elf
	"

for target in $_targets; do
	subpackages="$pkgname-$target:_install_subpkg $subpackages"
	# here only makedepends. The depends to binutils-$target and gcc-$target
	# will be set in the subpkg newlib-$target
	makedepends="binutils-$target gcc-$target $makedepends"
done

builddir="$srcdir/newlib-esp32-esp-$_tag"

# This package installs two version of the newlib library (regular and nano),
# following the model of the newlib package it is build upon and consitant
# with what Espressif does.

build() {
	for target in $_targets; do
		mkdir -p "$builddir/build-$target" "$builddir/build-$target-nano"
		# nano version
		cd "$builddir/build-$target-nano"
		# configure flags taken from https://github.com/espressif/crosstool-NG/blob/3393c69b560882e020ff7f95816be27f6fe9f8ac/samples/xtensa-esp32-elf/crosstool.config#L43
		export CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections -fshort-wchar -mlongcalls"
		"$builddir"/configure \
				--target=$target \
				--prefix=/usr/ \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--with-newlib \
				--enable-multilib \
				--disable-newlib-io-c99-formats \
				--disable-newlib-supplied-syscalls \
				--enable-newlib-nano-formatted-io \
				--enable-newlib-reent-small \
				--enable-target-optspace \
				--enable-newlib-nano-malloc \
				--enable-newlib-retargetable-locking \
				--disable-newlib_wide_orient \
				--enable-newlib-iconv \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make

		# regular version
		cd "$builddir/build-$target"
		export CFLAGS_FOR_TARGET="-O2 -ffunction-sections -fdata-sections -fshort-wchar -mlongcalls"
		# configure flags taken from https://github.com/espressif/crosstool-NG/blob/3393c69b560882e020ff7f95816be27f6fe9f8ac/samples/xtensa-esp32-elf/crosstool.config#L32-L39
		# Resolved variables using
		# https://github.com/espressif/crosstool-NG/blob/3393c69b560882e020ff7f95816be27f6fe9f8ac/scripts/build/libc/newlib.sh
		# as follows:
		# CT_LIBC_NEWLIB_DISABLE_SUPPLIED_SYSCALLS=y --> --disable-newlib-supplied-syscalls
		# CT_LIBC_NEWLIB_REENT_SMALL=y --> --enable-newlib-reent-small
		# CT_LIBC_NEWLIB_IO_C99FMT=y --> --enable-newlib-io-c99-formats
		# CT_LIBC_NEWLIB_IO_LL=y --> --enable-newlib-io-long-long
		# CT_LIBC_NEWLIB_IO_FLOAT=y --> --enable-newlib-io-float
		# CT_LIBC_NEWLIB_IO_POS_ARGS=y --> --enable-newlib-io-pos-args
		# CT_LIBC_NEWLIB_WIDE_ORIENT=n --> seems to be unused
		"$builddir"/configure \
				--host=$CHOST \
				--target=$target \
				--prefix=/usr \
				--infodir=/deleteme/info \
				--mandir=/deleteme/man \
				--htmldir=/deleteme/html \
				--pdfdir=/deleteme/pdf \
				--enable-newlib-nano-malloc \
				--enable-newlib-retargetable-locking \
				--enable-newlib-iconv \
				--disable-newlib-supplied-syscalls \
				--enable-newlib-reent-small \
				--enable-newlib-io-c99-formats \
				--enable-newlib-io-float \
				--enable-newlib-io-pos-args \
				CC_FOR_TARGET="$target"-gcc \
				GCC_FOR_TARGET="$target"-gcc \
				AR_FOR_TARGET="$target"-ar \
				AS_FOR_TARGET="$target"-as \
				LD_FOR_TARGET="$target"-ld \
				NM_FOR_TARGET="$target"-nm \
				OBJDUMP_FOR_TARGET="$target"-objdump \
				RANLIB_FOR_TARGET="$target"-ranlib \
				READELF_FOR_TARGET="$target"-readelf \
				STRIP_FOR_TARGET="$target"-strip
		make
	done
}

package() {
	for target in $_targets; do
		depends="$depends $pkgname-$target"
	done
	mkdir -p "$pkgdir"
}

_install_subpkg() {
	local target
	target="${subpkgname#newlib-esp-}"
	pkgdesc="A C standard library implementation intended for $target"
	depends="binutils-$target gcc-$target $depends"
	# the newlib fork of Espressif cannot be installed alongside an upstream
	# version of newlib.
	provides="newlib-$target=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"
	# install nano version
	cd "$builddir/build-$target-nano"
	make -j1 DESTDIR="$subpkgdir" install
	# rename nano version libs
	cd "$subpkgdir/usr/$target/lib"
	find . -regex ".*\(c\|g\|m\|rdimon\|gloss\)\.a" | while read -r _file; do
		mv "$_file" "${_file%%.a}_nano.a"
	done
	mkdir -p "$subpkgdir/usr/$target/include/newlib-nano"
	mv "$subpkgdir/usr/$target/include/newlib.h" \
	   "$subpkgdir/usr/$target/include/newlib-nano/"

	# install regular version
	cd "$builddir/build-$target"
	make -j1 DESTDIR="$subpkgdir" install
	rm -rf "$subpkgdir/deleteme"

	# install custom nano specs, if existing
	if [ -e "$srcdir/$target-nano.specs" ]; then
		install -m 644 "$srcdir/$target-nano.specs" \
			"$subpkgdir/usr/$target/lib/nano.specs"
	fi

	case $target in
	xtensa-esp*)
		# For xtensa targets we share the headers (they do not change based
		# on CPU paramters) and only keep the machine code.
		depends="newlib-xtensa-none-elf $depends"
		rm -rf "${subpkgdir:?}/usr/$target/include"
		ln -s "/usr/xtensa-none-elf/include" "${subpkgdir:?}/usr/$target/include"
		;;
	xtensa-none-elf)
		# For unknown xtensa, we only keep the headers.
		rm -rf "${subpkgdir:?}/usr/xtensa-none-elf/lib"
	esac
}

sha512sums="
5c3c1b17fc64c17a4ab5419edf34bf7eb964bddd99f46bcdab176b8a8fe4f352126ba608208c5ac0c64407beaa157d704e58af74f182cc168521675892dc1b36  newlib-esp-4.3.0_20240109.tar.gz
96d797e2bf354f0f6e9b5183b5df104baf24e7814c75091fd59c7694009d30b228e20a3a9df052b5be958596b5537f644efb72ef71d3dbfc40b4aca2208fea6f  0001-getentropy-fix-compilation.patch
"
